package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
@Api(tags="01.资讯管理模块")
@Slf4j
//@Controller
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
    @ApiOperation("添加资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult add(NoticeAddParam noticeAddParam, SessionStatus sessionStatus){
        log.debug("noticeAddParam="+noticeAddParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @ApiOperation("资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery="+noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOs=noticeMapper.list(title,type);
        return JsonResult.ok(noticeListVOs);
    }
    @GetMapping("detail")
    @ApiOperation("资讯详情")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name="id",value="资讯编号",required = true,dataType = "Long")
    public JsonResult detail(Long id){
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }
    @PostMapping("del")
    @ApiOperation("删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult del(Long id){
        int num = noticeMapper.delete(id);
        if(num>0){
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @PostMapping("update")
    @ApiOperation("更新资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult update(@ApiIgnore NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam="+noticeUpdateParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.update(notice);
        if(num>0){
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
